<?php

namespace App\Http\Controllers\Home;

use App\Model\Article;
use App\Model\Column;
use App\User;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;

class ArticleController extends Controller
{
//    public $columns;
    public function __construct()
    {
        $this->middleware('auth',[
           'only'=>['create','store'],
        ]);
//        $this->columns=(new Column())->get();
//        dd($this->columns);

    }

    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
//        $articles=User::get();
//        dd($articles->toArray());
        $articles=Article::paginate(10);
//        $article=Article::get();
//        dd($article);
//          $columns=(new Column())->get();
        return view('home.article.index',compact('articles'));
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        return view('home.article.create');
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request ,Article $article )
    {
//        dd(auth()->id());
//        dd($request->toArray());
        $request->validate([
            'title'=>'required',
            'content'=>'required',
        ],
            [
                'title.required'=>'请输入文章标题',
                'content.required'=>'请输入文章内容',
            ]);
//        $data=$request->all();
//        $data['user_id']=auth()->id();
//        dd($data);
//            dd($request->toArray());
//            $article->save($request->all());
        $article->user ()->associate(auth ()->id ())->fill ($request->all ())->save();
       return back()->with('success','添加文章成功');
    }

    /**
     * Display the specified resource.
     *
     * @param  \App\Model\Article  $article
     * @return \Illuminate\Http\Response
     */
    public function show(Article $article)

    {

//        $iszan=$article->zans()->get()->contains(auth()->id());
//        $user=$article->zans()->with(['user'])->get();
//        $columns=(new Column())->get();
//      return view('home.article.show',compact('article','user'));
      return view('home.article.show',compact('article'));
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  \App\Model\Article  $article
     * @return \Illuminate\Http\Response
     */
    public function edit(Article $article)
    {
//                dd($article->toArray());
        return view('home.article.edit',compact('article'));
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \App\Model\Article  $article
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, Article $article)
    {

           $article->update($request->all());
           return redirect('/home/article/index')->with('success','文章修改成功');

    }

    public function destroy(Article $article)
    {
       $article->delete();
       return redirect('/home/article/index')->with('success','删除成功');
    }
//    public function zans(Article $article){
//
//        $article->zans()->toggle(auth()->id());
//        return back()->with('success','操作成功');
//    }



//    public function favorite(Article $article){
////            dd($article->favorite());
//        $article->favorites()->toggle(auth()->id());
//        return back()->with('success','操作成功');
//    }

}
